﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LybrixNursingCommon;
using LybrixNursingCRMDesktop.FrameWork;

namespace LybrixNursingCRMDesktop
{
    /// <summary>
    /// Interaction logic for BateAvot.xaml
    /// </summary>
    public partial class BateAvot : Page
    {
        private NursingDBContainer _entities;
        private NursingHome currentNursingHome = null;

        public BateAvot()
        {
            InitializeComponent();
            _entities = Repository.Entities;
            PerformSearch();
        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            NavigationCenter.ShowDetailPage(currentNursingHome, NavigationTarget.NursingHomeDetails, this);
            e.Handled = true;
        }

        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var dep = (DependencyObject)e.OriginalSource;
            while ((dep != null) && !(dep is ListViewItem))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }
            if (dep == null)
                return;
            currentNursingHome = (NursingHome)HomeNursingListView.ItemContainerGenerator.ItemFromContainer(dep);
        }

        private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
        {
       
        }

        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            HomeName.Value = string.Empty;
            HomeContact.Value = string.Empty;
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            PerformSearch();
        }

        public void PerformSearch()
        {
            IQueryable<NursingHome> nursingHomes = null;
            nursingHomes = (from x in _entities.NursingHomes select x);
            if (HomeName.GotValue)
                nursingHomes = nursingHomes.Where(DynamicExpressions.GetWhereCond<NursingHome>(HomeName.FieldID, "Contains", HomeName.Value));
            HomeNursingListView.ItemsSource = nursingHomes;
        }
    }
}
